package com.biz.primus.ms.payment.channel.allinpay.req;

import com.biz.primus.common.utils.JsonUtils;
import com.biz.primus.ms.payment.channel.allinpay.res.AllInPayCardAddDataRespVo;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;

import java.util.TreeMap;
import java.util.TreeSet;

/**
 * AllInPayCardAdd
 * 充值
 *
 * @author JKLiues
 * @date 2017年11月13日
 */
@Getter
@Setter
@Slf4j
public class AllInPayCardAdd extends AllInPayRequestBase {
    private static final String API_URL = "http://116.228.64.55:8080/aop/rest";

    /**
     * 是 AOP分配给应用的AppKey
     */
    private String app_key;
    /**
     * 是 API接口名称
     */
//    private String method = "allinpay.ppcs.cloud.cardsingletopup.add";
    private String method = "allinpay.ppcs.cardsingletopup.add";
    /**
     * 是 时间戳，格式为yyyyMMddHHmmss，例如：20110125010101。API服务端允许客户端请求时间误差为30分钟。
     */
    private String timestamp;
    /**
     * 是 API协议版本，可选值:1.0。
     */
    private String v = "1.0";

    /**
     * 是 签名版本号 1,2,3,4,5每次更新后+1递增
     */
    private String sign_v = "1";
    /**
     * 是 指定响应格式。默认xml,目前支持格式为xml,json
     */
    private String format = "json";
    /**
     * String	必须	合作机构订单号
     */
    private String order_id;
    /**
     * 充值卡号
     */
    private String card_id;
    /**
     * 产品号
     */
    private String prdt_no;
    /**
     * 充值金额
     */
    private String amount;
    /**
     * 充值途径
     */
    private String top_up_way;
    /**
     * 充值机构或充值人员
     */
    private String opr_id;
    /**
     * 备注
     */
    private String desn;

    public AllInPayCardAddDataRespVo execute(String sign) {
        TreeMap<String, Object> map = new TreeMap<>();
        map.put("app_key", this.app_key);
        map.put("method", this.method);
        map.put("timestamp", this.timestamp);
        map.put("v", this.v);
        map.put("sign", sign);
        map.put("sign_v", this.sign_v);
        map.put("format", this.format);
        map.put("order_id", this.order_id);
        map.put("card_id", this.card_id);
        map.put("prdt_no", this.prdt_no);
        map.put("amount", this.amount);
        map.put("top_up_way", this.top_up_way);
        map.put("opr_id", this.opr_id);
        map.put("desn", this.desn);
        RestTemplate restTemplate = getRestTemplate();
        String finalUrl = buildUrl(getApiUrl(), map);
        String forObject = restTemplate.getForObject(finalUrl, String.class);
        log.info("get card add:{} \nresp:{}", finalUrl, forObject);
        return JsonUtils.json2Obj(forObject, AllInPayCardAddDataRespVo.class);
    }

    @Override
    protected String getApiUrl() {
        return API_URL;
    }

    @Override
    public TreeSet<String> getSignParams() {
        return Sets.newTreeSet(Lists.newArrayList(
                "app_key",
                "method",
                "timestamp",
                "v",
                "sign_v",
                "format",
                "order_id",
                "card_id",
                "prdt_no",
                "amount",
                "top_up_way",
                "opr_id",
                "desn"
        ));
    }
}